﻿using fCloud.SensorApi.Models;
using fCloud.SensorApi.Services;
using Microsoft.AspNetCore.Mvc;

namespace fCloud.SensorApi.Controllers;
[ApiController]
[Route("api/[controller]/[action]")]
public class DatabaseController(IServiceScopeFactory scopeFactory):ControllerBase
{
    private readonly IRedisService? _redisService=scopeFactory.CreateScope().ServiceProvider.GetService<IRedisService>();
    [HttpPost]
    public async Task SetHashValue(SensorData sensorData)
    {
        await _redisService!.InsertSensorDataAsync(sensorData);
    }
    
    /// <summary>
    /// 根据ID获取用户详情
    /// </summary>
    /// <param name="id">用户唯一标识</param>
    /// <response code="200">成功返回用户对象</response>
    /// <response code="404">用户不存在</response>
    [HttpGet]
    public async Task<IActionResult> GetKeys()
    {
        var res = await _redisService!.ScanSensorKeysAsync();
        var list =  res
            .OrderByDescending(s => s.CreateTime)
            .Take(6)
            .ToList();
        var sensorNewResponse = new CommonApiResponse()
        {
            Code = 200,
            Message = "success",
            Data = list
        };
        return Ok(sensorNewResponse);
    }
}